<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动态表格搜索</title>
    <style>
      table,
      th,
      td {
        border: 1px solid #ccc;
        border-collapse: collapse;
        text-align: center;
        width: 800px;
        padding: 20px 30px;
      }
    </style>
  </head>
  <body>
    请您输入内容: <input type="text" id="search" /> <br />
    <br />
    <table>
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>admin</td>
          <td>25</td>
        </tr>
        <tr>
          <td>ben</td>
          <td>21</td>
        </tr>
        <tr>
          <td>ann</td>
          <td>23</td>
        </tr>
        <tr>
          <td>tom</td>
          <td>28</td>
        </tr>
        <tr>
          <td>anita</td>
          <td>22</td>
        </tr>
      </tbody>
    </table>

    <script>
      let trs = document.querySelectorAll('tbody tr')
      search.onkeyup = function () {
        let input = this.value
        trs.forEach(tr => {
          console.log(tr)
          if (tr.cells[0].innerHTML.includes(input)) {
            tr.style.display = ''
          } else {
            tr.style.display = 'none'
          }
        })
      }
    </script>
  </body>
</html>
